var experss = require('express');
var router = experss.Router();
var db = require("../public/javascripts/db");
var multer = require('multer');
var upload = multer();
var _ = require("lodash");

/* GET home page. */
router.post('/',upload.array(), function(req, res, next) {
    let data = {
        isSuccess: false,
        data: null,
        msg: '录入失败！'
    };
    
    /**
     * 这里做处理，防止sql注入
     * 使用lodash库重的trim函数除去前端传回的数据重可能包含的空格
     */
    let zcbh = _.trim(String(req.body.zcbh)); // 资产编号不一定有值
    let tmbh = _.trim(String(req.body.tmbh)); // 条码编号一定有值
    let zybbh = _.trim(String(req.body.zybbh));
    let type = _.trim(String(req.body.type));
    let name = _.trim(String(req.body.name));

    let vendor = _.trim(String(req.body.vendor));
    let model = _.trim(String(req.body.model));
    let depart = _.trim(String(req.body.depart));
    let departid = _.trim(String(req.body.departid));
    let workshop = _.trim(String(req.body.workshop));
    let workshopid = _.trim(String(req.body.workshopid));
    let user = _.trim(String(req.body.user));

    let contact = _.trim(String(req.body.contact));
    let location = _.trim(String(req.body.location));
    let note = _.trim(String(req.body.note));
    let recorderid = _.trim(String(req.body.recorderid));
    let recorder = _.trim(String(req.body.recorder));

    let tableName = 'equipmentinfo';
    let colName = '`zcbh`,`tmbh`,`zybbh`,`type`,`name`,`vendor`,`model`,`depart`,`departid`,`workshop`,`workshopid`,`user`,`contact`,`location`,`note`,`putindate`,`recorder`,`recorderid`';
    let colValue = "'" + zcbh +"','" + tmbh +"','" + zybbh +"','" + type +"'," 
    +  "'" + name + "','" + vendor + "','" + model + "','" + depart + "','" +  departid + "',"
    +  "'" + workshop +"','"+ workshopid + "','" + user +"','" + contact +"','" + location + "',"
    +  "'" + note +"','" + new Date().toLocaleDateString() + "','"+ recorder +"','" +  recorderid + "'";

   let changeTableName = "equipment_change_record";
   let changeColName = "`equipmentid`,`depart`,`departid`,`workshop`,`workshopid`,`user`,`contact`,`location`, `desc`,`recorderid`,`recorddate`";
   let changeColValue = "(select id from equipmentinfo WHERE tmbh=" + tmbh + "),'" + depart + "', '" + departid + "','" + workshop 
   +  "','" + workshopid + "','" + user + "','" + contact + "','" + location + "','" + note + "','" + recorderid 
   + "','" +( new Date().toLocaleDateString() )+ "'";

   console.log("到这里");
    db.insert(tableName, colName, colValue, function(err, rows) {
        if(err) {
            return res.json(data);
        } else {
            console.log(rows.affectedRows)
            if(rows.affectedRows > 0 ) {
                /**
                 * 需要将最初的资产信息存入变更表（equipment_change_record）中
                 * 在有设备变更时，第一条记录的应该是现在的资产信息
                 */
                db.insert(changeTableName, changeColName, changeColValue, function(err1, rows1) {
                    if(err1) {
                        return res.json(data);
                    } else {
                        if(rows1.affectedRows > 0) {                            
                            data.isSuccess = true;
                            data.msg = "录入成功";
                            return res.json(data);
                        } else {
                            return res.json(data);
                        }
                    }
                })
            } else {
                return res.json(data);
            }
        }
    })
});

module.exports = router;